Skip to content

Eigenen Debian Mirror mit apt-mirror erstellen

In diesem Artikel wird beschrieben, wie man einen eigenen Debian Mirror – in unserem Fall verwenden wir Debian wheezy, erstellt. Zuerst installieren wir die benötigten Pakete.

root:~# apt-get install apt-mirror apache2

Nun editieren wir die Datei /etc/apt/mirror.list, um die gewünschten Distributionen bzw. Architekturen zu spiegeln.

root:~# vi /etc/apt/mirror.list
....
deb http://ftp.us.debian.org/debian wheezy main contrib non-free
deb-src http://ftp.us.debian.org/debian wheezy main contrib non-free
....
deb-amd64 http://ftp.us.debian.org/debian wheezy main contrib non-free
deb-i386 http://ftp.us.debian.org/debian wheezy main contrib non-free

Mit dem Befehl apt-mirror erstellen wir uns nun den Debian Mirror auf unserem Server. Das wird eine weile dauern.

root:~# apt-mirror /etc/apt/mirror.list

Wenn alles erfolgreich durchgelaufen ist, haben wir unter /var/spool/apt-mirror/mirror/ftp.us.debian.org/debian unseren Debian Mirror. Danach erstellen wir unter /var/www ein Symlink auf unseren Debian Mirror.

root:~# ln -s /var/spool/apt-mirror/mirror/ftp.us.debian.org/debian /var/www/debian

Anschließend konfigurieren wir noch den Apache, damit der Mirror über http/https erreichbar ist. Dazu erstellen wir uns im Verzeichnis eine Datei /etc/apache2/conf.d/debmirror.conf mit folgendem Inhalt.

root:~# vi /etc/apache2/conf.d/debmirror.conf
# $File: debmirror.conf $

<IfModule mod_alias.c>
    Alias /debian /var/www/debian
</IfModule>

<Directory /var/www/debian>
    Options +Indexes +FollowSymLinks
    AllowOverride None
    Order allow,deny
    allow from all
</Directory>

# vim: syntax=apache ts=4 sw=4 sts=4 sr noet

Damit die Änderungen vom Apache übernommen werden, wird die neue Konfiguration neu eingelesen.

root:~# service apache2 reload
[ ok ] Reloading web server config: apache2.

Wer möchte, kann noch einen Symlink stable im Debian Mirror erstellen, welcher in unserem Fall auf wheezy zeigt.

root:~# cd /var/spool/apt-mirror/mirror/ftp.us.debian.org/debian/dists && ln -s wheezy stable

Damit unserer Debian Mirror synchron bleibt, werden wir noch ein Cronjob erstellen, welcher den Mirror periodisch synchronisiert.

root:~# crontab -l
....
0 0 * * * /usr/bin/apt-mirror /etc/apt/mirror.list >/var/spool/apt-mirror/var/cron.log 2>/var/spool/apt-mirror/var/cron.err

Ich habe ein Skript geschrieben, was im Fall eines Fehlers Nachrichten an eine Email Adresse sendet. Hier das Skript:

#!/bin/bash
# *************************************************************
# $File: debmirror.sh $
# $Author: mschulz $ - $Date: 2013-07-16 14:57:49 +0200 (Di, 16 Jul 2013) $
# $HeadURL: http://svn.tuxnet24.de/operation/linux/bash/debmirror.sh $ - $Revision: 247$
# $Description: Script to synchronize a Debian mirror. $
# *************************************************************

# Path to the config file of apt-mirror
confile="/etc/apt/mirror.list"

# Path to the error logfile
logerr="/tmp/debmirror.err"

# Path to the cronjob logfile
logcron="/var/spool/apt-mirror/var/cron.log"

# The notify mail address
notify="root@localhost"

# The subject of the notify message
subject="[$(hostname)] $(basename $0)"

# Send notify message on success..?
notifyonsuccess="Off" # On/Off

# Language settings
declare -A msg

# German language
msg[de,0]="Der Debian Spiegel wurde erfolgreich synchronisiert."
msg[de,1]="Der Debian Spiegel konnte nicht synchronisiert werden."

# English language
msg[en,0]="The Debian mirror was successfully synchronized."
msg[en,1]="The Debian mirror could not be synchronized."

# Get the system language
locale=$(echo $LANG|cut -c-2)

# *************************************************************
# This function send a notify message.
#
# @param string $1 The notify mail address
# @param string $2 The notify message
# @param string $3 The notify subject
# @param string $4 The path to the report logfile
# @return void
#
function f_notify () {

local notify="$1"
local message="$2"
local subject="$3"
local logfile="$4"

# Create content of the mail body
local header="$( head -n7 $0 | grep -v '/bin/bash' | sed 's/^# //g' )"
local logtrace=$([ -f "$logfile" ] && cat $logfile 2>/dev/null || echo -n)

# Send notify message
echo -e "${header}\n\n*${message}*\n\n${logtrace}\n\n-- \n" | mail -s "${subject}" $notify

}

# *************************************************************
# MAIN

# Get a clean exit
trap "rm -f ${logerr}" EXIT

# Set the default language, if not defined
if ! echo "${locale}" | egrep -q 'de|en'; then locale="en"; fi

# Sync Debian Mirror
if /usr/bin/apt-mirror ${confile} >${logcron} 2>${logerr}; then
    if [ "${notifyonsuccess}" = "On" ]; then
        f_notify "${notify}" "${msg[$locale,0]}" "${subject} OK" ""
    fi
else
    f_notify "${notify}" "${msg[$locale,1]}" "${subject} ERROR" "${logerr}"
fi

# *************************************************************
# EOF

Den neuen Debian Mirror können wir nun in unserer source.list eintragen. YOUR_MIRROR_HOST muss natürlich mit der entsprechenden IP-Adresse bzw. Hostname ersetzt werden.

root:~# vi /etc/apt/sources.list
....
deb http://YOUR_MIRROR_HOST/debian/ wheezy main contrib non-free
deb-src http://YOUR_MIRROR_HOST/debian/ wheezy main contrib non-free

Mit apt-get update aktualisieren wir unsere Paketliste, welche von unserem eigenen Debian Mirror erstellt wird.

root:~# apt-get update

Dokumentation